/**
    IntroState.cpp
    Creates the Intro Screen for the game
*/

#include "IntroState.h"

IntroState IntroState::m_IntroState;



void IntroState::Init(sf::RenderWindow& window){

    std::cout << "Start intro State..." << std::endl;

    //Button Control variables
    sf::Vector2f buttonSize2 = sf::Vector2f(240,40);
    sf::Color buttonColor2 = sf::Color::White;
    sf::Vector2i buttonLocation = sf::Vector2i((window.getSize().x - buttonSize2.x)/2,400);

    // +++ VIEWS +++
    view.setCenter(window.getSize().x / 2, window.getSize().y / 2);
    view.setSize(window.getSize().x, window.getSize().y);
    window.setView(view);

    // +++ BACKGROUND IMAGE +++
    if(!bg.loadFromFile("resources/background.jpg")){
        std::cout << "Didn't Load" << std::endl;
    }
    background.setTexture(bg);
    background.setScale(1.28,1.28);


    //LOGO IMAGE
    logoTexture.loadFromFile("resources/logo.png");
    logo.setTexture(logoTexture);
    logo.scale(.7,.7);
    logo.setPosition((window.getSize().x - logo.getGlobalBounds().width) / 2 , 20);

    // +++ TGUI Stuff +++
    gui2.setWindow(window);
    gui2.setGlobalFont("resources/arial.ttf");

    //Start button
    tgui::Button::Ptr button3(gui2);
    button3->load("resources/Black.conf");
    button3->setSize(buttonSize2.x, buttonSize2.y);
    button3->setPosition(buttonLocation.x,buttonLocation.y);
    button3->setText("Start");
    button3->bindCallback(tgui::Button::LeftMouseClicked);
    button3->setCallbackId(1);
    button3->setTextColor(buttonColor2);

    std::cout << "Intro Init Complete... " << std::endl;

}

void IntroState::Cleanup(){}

void IntroState::Pause(){}
void IntroState::Resume(){}

void IntroState::Events(GameEngine* game, sf::RenderWindow& window){

    while (window.pollEvent(event)){
        // Close the window and end the game when X clicked

        if (event.type == sf::Event::Closed){
            std::cout << "...Quit Game..." << std::endl;
            game->Quit();
        }

        // === KEYBOARD EVENTS ===
        if (event.type == sf::Event::KeyPressed ){

            //RETURN KEY
            if (event.key.code == sf::Keyboard::Return){
                game->ChangeState( AManOfMapsState::Instance());
            }
        }


        gui2.handleEvent(event);


        // The callback loop
        tgui::Callback callback;

        while (gui2.pollCallback(callback)){

            // Transition to AManofMapsState State
            if (callback.id == 1){
                std::cout << "...Trying to Change State..." << std::endl;
                game->ChangeState( AManOfMapsState::Instance());

            }
        }
    }

}

void IntroState::Update(GameEngine* game, sf::RenderWindow& window){
}

void IntroState::Render(GameEngine* game, sf::RenderWindow& window){

    window.clear(sf::Color::White);

    window.draw(background);
    window.draw(logo);
    gui2.draw();
    window.display();


}
